<?php
/**
 * Created by PhpStorm.
 * User: w-pc
 * Date: 2017/03/02
 * Time: 22:53
 */

namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController {
    public function index(){
        $a_state = request('get','int','a_state',4);
        $page = request('get','int','page',1);
        $a_type = request('get','str','a_type');
        if($a_state==4){
            if($a_type=='') {
                $article_list = D('Article')->getArticleByList();
            }else{
                $article_list = D('Article')->get_TypeArticleList($a_type);
            }
            $select = request('post','str','select');
            if($select!=''){
                $article_list = D('Article')->get_LikeArticleByList($select);
                $this->assign('select',$select);
            }
        }else{
            $article_list = D('Article')->get_StateArticle($a_state);
        }
        $type_list = D('Article')->get_TypeList();
//        var_dump($type_list);
//        exit();
        //在出口统一做分页处理，这次把每个页数跳转链接和状态值都写好了再传过去，每次最多遍历五个页数，以当前页数为中心
        $page_list = pageKeyList($page,'article','a_state',$a_state,$article_list);
        $article_list = pageList($page,$article_list);
//        var_dump($page_list);exit();
        $this->assign('article_list',$article_list);
        $this->assign('page_list',$page_list);
        $this->assign('type_list',$type_list);
        $this->display();
    }
    public function show(){
        $a_id = request('get','int','a_id');
        if($a_id!=0){
            $tip = request('get','int','tip');

            if($tip==1){
                $this->redirect('/index.php?c=article&a=show&a_id='.$a_id);
            }else if($tip==2){
                $article = D('Article')->getArticleByOne($a_id);
                $this->assign('article',$article);
//                $this->assign('flag','1');
                $this->display();
            }
        }else{
//            $this->assign('flag','2');
            $this->display();
        }
    }
    public function dlfile($file_url, $save_to)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch,CURLOPT_URL,$file_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $file_content = curl_exec($ch);
        curl_close($ch);
        $downloaded_file = fopen($save_to, 'w');
        fwrite($downloaded_file, $file_content);
        fclose($downloaded_file);
    }
    public function delete(){
        $a_id = request('post','int','a_id');
        $article = D('Article')->getArticleByOne($a_id);
        $article['a_state'] = 3;
        D('Article')->set_UpdateArticle($a_id,$article);
//        return show(0,'已移至回收站');
    }
    public function update(){
        $a_id = request('post','int','a_id');
//        $flag = request('post','int','flag');
        if($a_id!=0){
            $article = D('Article')->getArticleByOne($a_id);
            $a_title = request('post','str','a_title');
            $a_addtime = request('post','str','a_addtime');
            $a_adduser = request('post','str','a_adduser');
            if ($_FILES['a_photo']['name']!=NULL){
//                echo $_FILES['a_photo']['name'];
//                exit();
                $arr['a_photo'] = upload_file($_FILES['a_photo'],$article['a_photo']);
            }
            $a_type = request('post','str','a_type');
            $a_state = request('post','str','a_state');
            $a_begin = request('post','str','a_begin');
            $a_text = request('post','str','a_text');
            $arr['a_title'] = $a_title;
            $arr['a_addtime'] = $a_addtime;
            $arr['a_adduser'] = $a_adduser;
            $arr['a_type'] = $a_type;
            if($a_state=='') {$a_state='0';}
            $arr['a_state'] = $a_state;
            $arr['a_begin'] = $a_begin;
            $arr['a_text'] = $a_text;
            $arr['a_id'] = $a_id;
            D('Article')->set_UpdateArticle($a_id,$arr);
            $this->redirect('/admin.php?c=article');
//            var_dump($arr);
//            exit();
        }else{
            $a_title = request('post','str','a_title');
            $a_addtime = request('post','str','a_addtime');
            $a_adduser = request('post','str','a_adduser');
            $a_type = request('post','str','a_type');
            $a_state = request('post','str','a_state');
            $a_begin = request('post','str','a_begin');
            $a_text = request('post','str','a_text');
            $a_photo = upload_file($_FILES['a_photo']);
            $arr['a_photo'] = $a_photo;
            $arr['a_title'] = $a_title;
            $arr['a_addtime'] = $a_addtime;
            $arr['a_adduser'] = $a_adduser;
            $arr['a_type'] = $a_type;
            if($a_state=='') {$a_state='0';}
            $arr['a_state'] = $a_state;
            $arr['a_begin'] = $a_begin;
            $arr['a_text'] = $a_text;
            $arr['a_id'] = $a_id;
            D('Article')->set_InsertArticle($arr);
            $this->redirect('/admin.php?c=article');
        }
    }
    public function themes(){
        $a_id = request('get','int','a_id');
        if($a_id!=0){
            $article = D('Article')->getArticleByOne($a_id);
            $this->assign('article',$article);
            $this->assign('a_id',$a_id);
            $this->display();
        }else{
            $this->display();
        }
    }
    public function themes2(){
        $this->display();
    }
    public function ajax_content(){
        $a_text = request('post','str','a_text','');
        $a_id = request('get','int','a_id',0);
        $article = D('Article')->getArticleByOne($a_id);
        $article['a_text'] = $a_text;
        D('Article')->set_UpdateArticle($a_id,$article);
    }
    /**
     * 文章置顶
     */
    public function stick(){
        $a_id = request('get','int','a_id');
        D('Article')->set_Stick($a_id);
        $this->redirect('/admin.php?c=article');
    }
    /**
     * 图片上传操作
     */
    public function upload(){
        $a_id = request('post','int','a_id');

            //全局变量
            $arrType=array('image/jpg','image/gif','image/png','image/bmp','image/jpeg');
            $max_size='500000';      // 最大文件限制（单位：byte）
            $upfile='./Public/file/images'; //图片目录路径
            $file=$_FILES['upfile'];
            if($_SERVER['REQUEST_METHOD']=='POST'){ //判断提交方式是否为POST
                if(!is_uploaded_file($file['tmp_name'])){ //判断上传文件是否存在
                    $this->assign('article','文件不存在！');
                    $this->redirect('/admin.php?c=article&a=show&a_id='.$a_id.'&tip=2');
                    exit;
                }
                if($file['size']>$max_size){  //判断文件大小是否大于500000字节
                    $this->assign('article','上传文件太大！');
                    $this->redirect('/admin.php?c=article&a=show&a_id='.$a_id.'&tip=2');
                    exit;
                }
                if(!in_array($file['type'],$arrType)){  //判断图片文件的格式
                    $this->assign('article','上传文件格式不对！');
                    $this->redirect('/admin.php?c=article&a=show&a_id='.$a_id.'&tip=2');
                    exit;
                }
                if(!file_exists($upfile)){  // 判断存放文件目录是否存在
                    mkdir($upfile,0777,true);
                }
                $imageSize=getimagesize($file['tmp_name']);
                $img=$imageSize[0].'*'.$imageSize[1];
                //采用时间戳命名
                $fname=rand().time();
                $ftype=explode('.',$fname);
                $fileinfo=pathinfo($file['name']);
//                var_dump($fileinfo['extension']);
//                exit();
                $picName=$upfile."/rain".$fname.'.'.$fileinfo['extension'];
                if(file_exists($picName)){
                    $this->assign('article','同文件名已存在！');
                    $this->redirect('/admin.php?c=article&a=show&a_id='.$a_id.'&tip=2');
                    exit;
                }
                if(!move_uploaded_file($file['tmp_name'],$picName)){
                    $this->assign('article','移动文件出错！');
                    $this->redirect('/admin.php?c=article&a=show&a_id='.$a_id.'&tip=2');
                    exit;
                }else{
//                echo $picName."<br>";
//                echo "<font color='#FF0000'>图片文件上传成功！</font><br/>";
//                echo "<font color='#0000FF'>图片大小：$img</font><br/>";
//                echo "图片预览：<br><div style='border:#F00 1px solid; width:200px;height:200px'>
//                    <img src=\"".$picName."\" width=200px height=200px>".$fname."</div>";
                    if($a_id!=0){
                        D('Article')->upload_Photo($a_id,$picName);
                        $this->assign('article','图片文件上传成功！');
                        $this->redirect('/admin.php?c=article&a=show&a_id='.$a_id.'&tip=2');
                        exit();
                    }else{
                        $arr['a_title'] = '';
                        $arr['a_addtime'] = '';
                        $arr['a_adduser'] = '';
                        $arr['a_photo'] = $picName;
                        $arr['a_type'] = '';
                        $arr['a_state'] = 1;
                        $arr['a_begin'] = '';
                        $arr['a_text'] = '';
                        $a_id = D('Article')->set_InsertArticle($arr);
                        $this->redirect('/admin.php?c=article&a=show&a_id='.$a_id.'&tip=2');
                        exit();
                    }
                }
            }
    }


}